********************************************************************************
* CREATE QY TAILS DATASET, USING Q DISTS, WITH Y TAIL PROBABILITIES PER MONTH
 
/*
*Run this block if stand alone. If commented out, it is because this will be run from _ricardo_master.do
set more off, perm
clear
graph drop _all
local dir "/Users/rreis/Dropbox/05Shared_folders/HRR/HRR_InflDisaster/Ricardo"  // RR's directory
cd `dir'
local region = "US" 	// set region US or EZ
local updateIteration "Mar2022"
*/

local region `1'
local updateIteration `2'

*Redo monthly choice to make sure it is, and drop empty variables
u "$input/`region'_dist_Y_monthly", clear

gen day = day(date_stata)
gen month = month(date_stata)
gen year = year(date_stata)
sort year month day
    collapse (first) date_stata, by(year month)
    keep date_stata
    merge 1:m date using "$input/`region'_dist_Y_monthly.dta"
    keep if _merge == 3
    drop _merge
*drop I J K L M N O

********************************************************************************

// Generate yoy upper tails
generate y6y = frequency if dist_identifier=="YQ6" 
generate uptails_6y = y6y if support>=.04
egen Ytail4_y6 = total(uptails_6y), by(date_stata)
replace uptails_6y=. if support<0.05
egen Ytail5_y6 = total(uptails_6y), by(date_stata)
drop uptails_6y y6y

generate y7y = frequency if dist_identifier=="YQ7" 
generate uptails_7y = y7y if support>=.04
egen Ytail4_y7 = total(uptails_7y), by(date_stata)
replace uptails_7y=. if support<0.05
egen Ytail5_y7 = total(uptails_7y), by(date_stata)
drop uptails_7y y7y

generate y8y = frequency if dist_identifier=="YQ8" 
generate uptails_8y = y8y if support>=.04
egen Ytail4_y8 = total(uptails_8y), by(date_stata)
replace uptails_8y=. if support<0.05
egen Ytail5_y8 = total(uptails_8y), by(date_stata)
drop uptails_8y y8y

generate y9y = frequency if dist_identifier=="YQ9" 
generate uptails_9y = y9y if support>=.04
egen Ytail4_y9 = total(uptails_9y), by(date_stata)
replace uptails_9y=. if support<0.05
egen Ytail5_y9 = total(uptails_9y), by(date_stata)
drop uptails_9y y9y

generate y10y = frequency if dist_identifier=="YQ10" 
generate uptails_10y = y10y if support>=.04
egen Ytail4_y10 = total(uptails_10y), by(date_stata)
replace uptails_10y=. if support<0.05
egen Ytail5_y10 = total(uptails_10y), by(date_stata)
drop uptails_10y y10y

// Generate yoy bottom tails
generate y6y = frequency if dist_identifier=="YQ6" 
generate dotails_6y = y6y if support<=0
egen Ytail0_y6 = total(dotails_6y), by(date_stata)
replace dotails_6y=. if support>-0.01
egen Ytailm1_y6 = total(dotails_6y), by(date_stata)
drop dotails_6y y6y

generate y7y = frequency if dist_identifier=="YQ7" 
generate dotails_7y = y7y if support<=0
egen Ytail0_y7 = total(dotails_7y), by(date_stata)
replace dotails_7y=. if support>-0.01
egen Ytailm1_y7 = total(dotails_7y), by(date_stata)
drop dotails_7y y7y

generate y8y = frequency if dist_identifier=="YQ8" 
generate dotails_8y = y8y if support<=0
egen Ytail0_y8 = total(dotails_8y), by(date_stata)
replace dotails_8y=. if support>-0.01
egen Ytailm1_y8 = total(dotails_8y), by(date_stata)
drop dotails_8y y8y

generate y9y = frequency if dist_identifier=="YQ9" 
generate dotails_9y = y9y if support<=0
egen Ytail0_y9 = total(dotails_9y), by(date_stata)
replace dotails_9y=. if support>-0.01
egen Ytailm1_y9 = total(dotails_9y), by(date_stata)
drop dotails_9y y9y

generate y10y = frequency if dist_identifier=="YQ10" 
generate dotails_10y = y10y if support<=0
egen Ytail0_y10 = total(dotails_10y), by(date_stata)
replace dotails_10y=. if support>-0.01
egen Ytailm1_y10 = total(dotails_10y), by(date_stata)
drop dotails_10y y10y

// Collapse and drop all other variables
collapse date Ytail4_y6 Ytail4_y7 Ytail4_y8 Ytail4_y9 Ytail4_y10 Ytail5_y6 Ytail5_y7 Ytail5_y8 Ytail5_y9 Ytail5_y10 Ytail0_y6 Ytail0_y7 Ytail0_y8 Ytail0_y9 Ytail0_y10 Ytailm1_y6 Ytailm1_y7 Ytailm1_y8 Ytailm1_y9 Ytailm1_y10, by(date_stata)

// Generate year month variable
g date_ym = mofd(date_stata)
format date_ym %tm

//Create good labels
label data "Dataset of tail probabilities, at 5- and 10-year horizon, at -1,0,4,5"
label variable Ytail4_y6 	"YTail CDF: 1 - Q(pi_6=4)"
label variable Ytail4_y7 	"YTail CDF: 1 - Q(pi_7=4)"
label variable Ytail4_y8 	"YTail CDF: 1 - Q(pi_8=4)"
label variable Ytail4_y9 	"YTail CDF: 1 - Q(pi_9=4)"
label variable Ytail4_y10 	"YTail CDF: 1 - Q(pi_10=4)"
label variable Ytail5_y6 	"YTail CDF: 1 - Q(pi_6=5)"
label variable Ytail5_y7 	"YTail CDF: 1 - Q(pi_7=5)"
label variable Ytail5_y8 	"YTail CDF: 1 - Q(pi_8=5)"
label variable Ytail5_y9 	"YTail CDF: 1 - Q(pi_9=5)"
label variable Ytail5_y10 	"YTail CDF: 1 - Q(pi_10=5)"
label variable Ytail0_y6 	"YTail CDF: Q(pi_6=0)"
label variable Ytail0_y7 	"YTail CDF: Q(pi_7=0)"
label variable Ytail0_y8 	"YTail CDF: Q(pi_8=0)"
label variable Ytail0_y9 	"YTail CDF: Q(pi_9=0)"
label variable Ytail0_y10 	"YTail CDF: Q(pi_10=0)"
label variable Ytailm1_y6 	"YTail CDF: Q(pi_6=-1)"
label variable Ytailm1_y7 	"YTail CDF: Q(pi_7=-1)"
label variable Ytailm1_y8 	"YTail CDF: Q(pi_8=-1)"
label variable Ytailm1_y9 	"YTail CDF: Q(pi_9=-1)"
label variable Ytailm1_y10 	"YTail CDF: Q(pi_10=-1)"
label variable date_ym		"Date in plot format"
label variable date_stata	"Date in Stata format"
label variable date	"Date in original format"
order date_ym , after(date_stata)

// save it
sleep 100
save "$input/`region'_YQtails.dta", replace
